//模块：
var compare = LArk.widget.bindClass(LArk.widget.BaseWidget.extend({
    options: {
        //直接添加到index
        view: {
            type: "window",
            url: 'view.html',
            windowOptions: {
                width: 250,
                height: 500,
                "position": {"top": 160,"left": 10}
            }
        },
    },
    //layersObj : {},
    create: function () {
        var inhtml = '<div id="centerDivEx" style="position:absolute;left:0px;top:0px;border:1px solid #ccc;bottom: 0px;width:50%;overflow: hidden;">'
            + '<div id="cesiumContainerEx" style="height:100%;width:100%;overflow: hidden;"></div>'
            + '</div>';
        $("#mainContainer").append(inhtml);
        $("#centerDiv").css({
            position: "absolute",
            width: "50%",
            height: "100%",
            top:0,
            right:0
        });
         
        var configdata = this.viewer.LArkgis.getConfig();
        configdata.controls = [];

        this.viewerEx = LArk.createMap({
            id: 'cesiumContainerEx',
            data: configdata,
            "homeButton": false,
            "geocoder": false,
            "sceneModePicker": false,
            "navigationHelpButton": false,
            "vrButton": false,
            "fullscreenButton": false,
            "baseLayerPicker": true,
            success: function (_viewer, gisdata, jsondata) {//地图成功加载完成后执行

            }
        });
        var that = this;
        this.viewer.scene.morphComplete.addEventListener(function (event) {//切换场景前事件 
            if (that.viewer.scene.mode === Cesium.SceneMode.SCENE2D) { 
                that.viewerEx.scene.screenSpaceCameraController.enableTilt = false;
            }
            else {  
                that.viewerEx.scene.screenSpaceCameraController.enableTilt = true;
            }
        });
    },
    typeFP: false,//true:垂直，false水平
    //每个窗口创建完成后调用
    winCreateOK: function (opt, result) {
        var that = this;
        if (this.config && this.config.style) {
            $(".mapCompareBar").addClass(this.config.style);
        }
        $("#btn_mapCompare_close").click(function () {
            that.disableBase();
        });
        //设置右侧图层界面
        // compare.setLayerList();
    },
    //激活插件
    activate: function () {
        $('#btn_mapSwipe_close').trigger('click');
        if (this.typeFP) {
            $("#centerDiv").css({
                position: "absolute",
                height: "50%",
                width: "100%"
            });
        } else {
            $("#centerDiv").css({
                position: "absolute",
                height: "100%",
                width: "50%"
            }); 
        }
        $("#centerDivEx").show();
        this.invalidateSize();
        this.viewer.camera.changed.addEventListener(this._map_extentChangeHandler, this);
        this.viewer.camera.percentageChanged = 0.01;
        this.viewerEx.camera.changed.addEventListener(this._mapEx_extentChangeHandler, this);
        this.viewerEx.camera.percentageChanged = 0.01;
        this._map_extentChangeHandler();
    },
    updateLayerVisible: function (cfg, visible) {
        if (!this.isActivate) return;
        var layersCfg = this.viewerEx.gisdata.config.operationallayers;
        if (layersCfg && layersCfg.length > 0) {
            for (var i = 0; i < layersCfg.length; i++) {
                var item = layersCfg[i];
                if (item.id == cfg.id && item.pid == cfg.pid && item.name == cfg.name) {
                    if (item._layer)
                        item._layer.setVisible(visible);
                    break;
                }
            }
        }
    },
    //释放插件
    disable: function () {
        this.viewer.camera.changed.removeEventListener(this._map_extentChangeHandler, this);
        this.viewerEx.camera.changed.removeEventListener(this._mapEx_extentChangeHandler, this);
        $("#centerDivEx").hide();
        $("#centerDiv").css({
            position: "",
            height: "100%",
            width: "100%"
        });
        this.invalidateSize();
    },
    invalidateSize: function () {},
    _map_extentChangeHandler: function (e) {
        this.viewerEx.camera.changed.removeEventListener(this._mapEx_extentChangeHandler, this);
        this.updateView(this.viewer, this.viewerEx);
        this.viewerEx.camera.changed.addEventListener(this._mapEx_extentChangeHandler, this);
    },
    _mapEx_extentChangeHandler: function (e) {
        this.viewer.camera.changed.removeEventListener(this._map_extentChangeHandler, this);
        this.updateView(this.viewerEx, this.viewer);
        this.viewer.camera.changed.addEventListener(this._map_extentChangeHandler, this);
    },
    //“变化屏”viewerChange变化，将“被更新屏”viewerUpdate同步更新
    updateView: function (viewerChange, viewerUpdate) {
        var point = LArk.point.getCameraView(viewerChange);
        viewerUpdate.camera.setView({
            destination: Cesium.Cartesian3.fromDegrees(point.x, point.y, point.z),
            orientation: {
                heading: Cesium.Math.toRadians(point.heading),
                pitch: Cesium.Math.toRadians(point.pitch),
                roll: Cesium.Math.toRadians(point.roll)
            }
        });
    }
}));